package main

import (
	"fmt"
	"time"
)

func main() {

	ch := make(chan any)
	go foo(ch, true)
	go foo(ch, false)

	// 声明一个只能写入数据的通道类型, 并赋值为ch
	var chSendOnly chan<- any = ch
	// 声明一个只能读取数据的通道类型, 并赋值为ch
	var chRecvOnly <-chan any = ch
	go bar(chSendOnly)
	go gkd(chRecvOnly)

	time.Sleep(time.Second)
}

// foo 的channel是一个双向通道；可读可写
func foo(ch chan any, flag bool) {
	if flag {
		ch <- 0
	} else {
		rsp := <-ch
		fmt.Println("rsp is", rsp)
	}
}

func bar(ch chan<- any) {
	ch <- 998
}

func gkd(ch <-chan any) {
	info := <-ch
	fmt.Println("info is ", info)
}
